<?php
namespace php\geo\googlemaps;

class GeoCodeResponse {

    const STATUS_OK = "OK";
    const STATUS_ZERO_RESULTS = "ZERO_RESULTS";
    const STATUS_OVER_QUERY_LIMIT = "OVER_QUERY_LIMIT";
    const STATUS_REQUEST_DENIED = "REQUEST_DENIED";
    const STATUS_INVALID_REQUEST = "INVALID_REQUEST";

    private $xmlObject;
    private $status;
    private $results;

    /*
     * Constructs a new object receives an xml string
     *
     */
    public function __construct($xmlData) {
        $this->xmlObject = new \SimpleXMLElement($xmlData);
        $this->results = array();
        $this->parseResponse();
    }

    private function parseResponse() {
        $this->parseStatus();
        if ($this->status == self::STATUS_OK) {
            $this->parseResults();
        }
    }

    private function parseStatus() {
        $this->status = strval($this->xmlObject->status);
    }

    private function parseResults() {
        foreach ($this->xmlObject->result as $result) {
            $this->results[] = new GeoCodeAddress($result);
        }
    }

    public function getStatus() {
        return $this->status;
    }

    /*
     * @return array of GeoCodeAddress
     */
    public function getResults() {
        return $this->results;
    }

    public function getFirstResult() {
        return $this->results[0];
    }

    public function hasResults() {
        return count($this->results)>0;
    }


}
